Skip to content

include/stdio.h: Hide the kernel file system types from kernel-build programs. - #20565

Merged
acassis merged 1 commit into
apache:masterfrom
casaroli:libc-stdio-kernel-types
Oct 13, 2026
Merged

acassis merged 1 commit into
apache:masterfrom
casaroli:libc-stdio-kernel-types

Conversation

@casaroli

@casaroli casaroli commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Summary

stdio.h includes nuttx/fs/fs.h and nuttx/lib/lib.h. So every program that includes stdio.h also sees the kernel's struct file, struct inode and the other file system types. A program that defines its own struct file does not compile: error: redefinition of 'struct file'.

This PR makes stdio.h stop including nuttx/lib/lib.h in all builds (review), and nuttx/fs/fs.h in the user space of a kernel build (CONFIG_BUILD_KERNEL and not __KERNEL__), where programs cannot use those types. Kernel code, and flat and protected builds, still get fs.h. Removing it for every build breaks many files that rely on the indirect include; that can be a later PR.

stdio.h now defines what it needs itself: the fopencookie() callback types, struct file_struct and its __FS_FLAG_* flags, which were in nuttx/fs/fs.h, and the declaration of lib_get_stream(), which stdin, stdout and stderr expand to. struct streamlist moves from nuttx/fs/fs.h to nuttx/tls.h, where struct task_info_s holds the stream list. nuttx/tls.h includes stdio.h for struct file_struct. No nuttx/fs/cookie.h is needed: stdio.h no longer reaches nuttx/tls.h (the old path was stdio.h -> lib.h -> kmalloc.h -> sched.h -> tls.h).

The files that used lib.h, kmalloc.h, stdlib.h or unistd.h names through stdio.h now include what they use (69 files in drivers, arch, boards, fs, sched and libs).

The old nxstyle errors of the changed files, and the tools/nxstyle.c fix they need, were merged in #20621. This PR is now the stdio.h commit only.

Impact

Code that used names from nuttx/lib/lib.h, nuttx/kmalloc.h, nuttx/sched.h, stdlib.h or unistd.h through stdio.h must include those headers itself. This includes 44 apps files, fixed in apache/nuttx-apps#3839. Its description lists them and how they were found: every apps C file compiled against master and against this PR, for four configurations.

Testing

A program that defines its own struct file and includes <stdio.h>, built outside the tree against the qemu-armv8a:knsh export package and run in QEMU 11:

Before:

ownfile.c:5:8: error: redefinition of 'struct file'

After:

nsh> ownfile
ownfile: struct file value 42

Builds with -Wno-cpp -Werror and apps master. The kernel builds marked (i) also run make export and the apps make import.

  • Kernel builds: qemu-armv8a:knsh (i), imx93-evk:knsh, qemu-armv7a:knsh (i), sabre-6quad:netknsh (i), rv-virt:knsh, rv-virt:knetnsh64 (i), rv-virt:kfb64, rv-virt:knsh_paging, icicle:knsh, canmv230:knsh, esp32s3-devkit:kernel_n8r2, mps3-an547:knsh.
  • Flat and protected: lm3s6965-ek:qemu-flat, lm3s6965-ek:qemu-protected, mps2-an500:nsh, qemu-armv8a:nsh, esp32-devkitc:nsh, esp32-devkitc:adc, rv-virt:nsh64, sabre-6quad:nsh, stm32f4discovery:nsh, spresense:wifi_smp, nucleo-f302r8:ihm07m1_b16, esp32c6-devkitc:mpu60x0, ox64:adc, mikroe-stm32f4:fulldemo, tm4c1294-launchpad:nsh, xmc4800-relax:nsh, icicle:rpmsg-ch2, and 19 more configurations of a sweep chosen for many drivers and apps.
  • Fail only in the apps files above: esp32c6-devkitc:oa_tc6, qemu-armv7a:full (its libcxx and mbedtls errors are on master too), stm32f746g-disco:dropbear, linum-stm32h753bi:zbus.
  • Fail on master too: sama5d4-ek:knsh, sama5d4-ek:nxwm (sam_mck_frequency), sim:nsh on macOS.

tools/checkpatch.sh -c -u -m -g passes.

depends-on: apache/nuttx-apps/pull/3839

Comment thread include/stdio.h
@github-actions github-actions Bot added Area: File System File System issues Area: OS Components OS Components issues Size: M The size of the change in this PR is medium labels Oct 9, 2026
@github-actions

github-actions Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report

No memory changes detected for:

@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown

🔗 Cross-repo PR dependencies

The read-only Build run reported the following dependent PR(s) and fetched head SHA(s):

CI run: https://github.com/apache/nuttx/actions/runs/37943764063

Comment thread include/nuttx/fs/cookie.h Outdated
Comment thread include/stdio.h
Comment thread include/nuttx/fs/cookie.h Outdated
Comment thread include/stdio.h Outdated
@casaroli

Copy link
Copy Markdown
Contributor Author

@linguini1 I moved the nxstyle commit, and the tools/nxstyle.c fix it needs, to #20621. When it is merged, this PR keeps only the stdio.h commit.

@github-actions

Copy link
Copy Markdown

🔗 Cross-repo PR dependencies

The read-only Build run reported the following dependent PR(s) and fetched head SHA(s):

CI run: https://github.com/apache/nuttx/actions/runs/38248342757

acassis
acassis previously approved these changes Oct 12, 2026
@acassis

acassis commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

@casaroli please fix:

netpkt_ethercat.c: In function 'netpkt_ethercat_main':
Error: netpkt_ethercat.c:89:16: error: implicit declaration of function 'atoi' [-Werror=implicit-function-declaration]
   89 |       repeat = atoi(argv[2]);
      |                ^~~~
Error: netpkt_ethercat.c:95:7: error: implicit declaration of function 'close'; did you mean 'pclose'? [-Werror=implicit-function-declaration]
   95 |       close(sockfd);
      |       ^~~~~
      |       pclose
Error: netpkt_ethercat.c:125:7: error: implicit declaration of function 'usleep' [-Werror=implicit-function-declaration]
  125 |       usleep(1000);
      |       ^~~~~~

xiaoxiang781216 pushed a commit that referenced this pull request Oct 13, 2026
#20565 adds include directives to these files, and the
check job runs nxstyle on the whole of every changed file.  Their nxstyle errors
are older than the change: braces, case labels and comments in the
wrong column, missing blank lines after declarations, and long lines.

Only the layout changes.  With all white space and comment margins
removed, every file is identical to the old one, except two: iconv()
and stm32_touchscreen.c get braces around one-line statements, and
getpass() gets a break aligned.  The object code of the three libc files
is identical before and after on qemu-armv8a:nsh with LIBC_LOCALE.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
@casaroli
casaroli force-pushed the libc-stdio-kernel-types branch from 77d0912 to 516eb56 Compare October 13, 2026 04:25
@github-actions github-actions Bot added Size: M The size of the change in this PR is medium and removed Size: XL The size of the change in this PR is very large. Consider breaking down the PR into smaller pieces. labels Oct 13, 2026
@github-actions

Copy link
Copy Markdown

🔗 Cross-repo PR dependencies

The read-only Build run reported the following dependent PR(s) and fetched head SHA(s):

CI run: https://github.com/apache/nuttx/actions/runs/38346358795

@casaroli
casaroli dismissed stale reviews from xiaoxiang781216 and acassis via c393b35 October 13, 2026 05:25
@casaroli
casaroli force-pushed the libc-stdio-kernel-types branch 2 times, most recently from c393b35 to 9a38e62 Compare October 13, 2026 06:34
…programs.

stdio.h included nuttx/fs/fs.h and nuttx/lib/lib.h, so every program
that includes stdio.h saw the kernel's struct file, struct inode and the
other file system types.  A program that defines its own struct file
does not compile.

stdio.h no longer includes nuttx/lib/lib.h in any build, and includes
nuttx/fs/fs.h only for kernel code and for flat and protected builds.
In the user space of a kernel build (CONFIG_BUILD_KERNEL and not
__KERNEL__) programs cannot use the file system types anyway, so they
do not see them.  Doing the same for flat and protected builds breaks
many files that rely on the indirect include.

stdio.h now defines what it needs itself: the fopencookie() callback
types, struct file_struct and its __FS_FLAG_* flags, which were in
nuttx/fs/fs.h, and the declaration of lib_get_stream(), which stdin,
stdout and stderr expand to.  struct streamlist moves from
nuttx/fs/fs.h to nuttx/tls.h, where struct task_info_s holds the stream
list.  nuttx/tls.h includes stdio.h for struct file_struct.

The files that used kernel types or other headers through stdio.h now
include what they use.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
@casaroli
casaroli force-pushed the libc-stdio-kernel-types branch from 9a38e62 to 0c8dcf1 Compare October 13, 2026 07:43
@acassis
acassis merged commit f6e5be7 into apache:master Oct 13, 2026
29 of 52 checks passed
@github-actions

Copy link
Copy Markdown

🔗 Cross-repo PR dependencies

The read-only Build run reported the following dependent PR(s) and fetched head SHA(s):

CI run: https://github.com/apache/nuttx/actions/runs/38364521591

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arch: arm Issues related to ARM (32-bit) architecture Arch: risc-v Issues related to the RISC-V (32-bit or 64-bit) architecture Arch: simulator Issues related to the SIMulator Arch: xtensa Issues related to the Xtensa architecture Area: Bluetooth Board: arm Board: mips Board: risc-v Board: sparc Board: xtensa Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants